//
//  CustomTableViewCell.m
//  UI_10_ CustomTableViewCell
//
//  Created by li cheng on 16/7/27.
//  Copyright © 2016年 li cheng. All rights reserved.
//


#import "CustomTableViewCell.h"
#import "UIView+Frame.h"
// 使用@class声明的类,需要在.m中import
#import "NewsObject.h"
@interface CustomTableViewCell()

@end
@implementation CustomTableViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
// 自定义tableViewCell的初始化方法
// 注意:不要使用initWithFrame
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // tableViewCell的默认大小, 320, 44
        // 初始化时,cell的frame是初始值,不能为自定义视图布局
        NSLog(@"初始化时,cell的frame%@", NSStringFromCGRect(self.frame));
        // 添加View
        _customView = [[UIView alloc] initWithFrame:CGRectZero];
        _customView.backgroundColor = [UIColor redColor];
//        [self.contentView addSubview:_customView];
        // 初始化方法中,只负责创建
        // 3.创建的子视图应该添加在哪个父视图
        _imageViewOfNew = [[UIImageView alloc] init];
        _imageViewOfNew.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_imageViewOfNew];
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_titleLabel];
        _descriptionLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_descriptionLabel];
        _descriptionLabel.backgroundColor = [UIColor purpleColor];
        // 强调,Label上面不可以加空间
        _numberCountsLabel = [[UILabel alloc] init];
        _numberCountsLabel.backgroundColor = [UIColor orangeColor];
        [_descriptionLabel addSubview:_numberCountsLabel];
    }
    return self;
}
// 布局子视图以及她本身
- (void)layoutSubviews{
    // 千万不要忘记super
    [super layoutSubviews];
    NSLog(@"%s时,cell的frame-----------%@", __FUNCTION__, NSStringFromCGRect(self.frame));
    self.customView.frame= CGRectMake(0, 0, self.bounds.size.width, 22)
    ;
    // layoutSubView中负责布局
    // 不要使用cell的frame去进行视图的布局
    // 使用contentView的frame布局
//    _customView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 100);
    _imageViewOfNew.frame = CGRectMake(5, 5, self.contentView.lc_width / 3, self.contentView.lc_height - 10);
    CGFloat titleLabelX = _imageViewOfNew.lc_x + _imageViewOfNew.lc_width + 10;
    _titleLabel.frame = CGRectMake(titleLabelX, 5, self.contentView.lc_width - titleLabelX - 5, self.contentView.lc_height / 5 * 2 - 5);
    CGFloat descriptionLabelY = 5 + _titleLabel.lc_height + 5;
    _descriptionLabel.frame = CGRectMake(titleLabelX, descriptionLabelY, self.contentView.lc_width - titleLabelX - 5, self.contentView.lc_height / 5 * 3 - 5);
    CGFloat numberCountsX = _descriptionLabel.lc_width * 3 / 5;
    CGFloat numberCountWidth = _descriptionLabel.lc_width * 2 / 5;
    CGFloat numberCountsHeight = _descriptionLabel.lc_height / 2;
    // 1.判断父视图是不是_descriptionLabel
    if (_numberCountsLabel.superview == _descriptionLabel){
//         改变_numberCountLabel的父视图
//    [_descriptionLabel addSubview:_numberCountsLabel];
    _numberCountsLabel.frame = CGRectMake(numberCountsX, numberCountsHeight, numberCountWidth, numberCountsHeight);
    // 视图坐标系转化
    CGRect rect = [_numberCountsLabel.superview convertRect:_numberCountsLabel.frame toView:self.contentView];
    _numberCountsLabel.frame = rect;
    [self.contentView addSubview:_numberCountsLabel];
    }
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)setNewsObject:(NewsObject *)newsObject{
    _newsObject = newsObject;
    _imageViewOfNew.image = [UIImage imageNamed:newsObject.newsImage];
    _titleLabel.text = newsObject.newsTitle;
    _descriptionLabel.text = newsObject.newsDescription;
    _numberCountsLabel.text = newsObject.newsNumberCount;

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
